package cp5_jdbc.user_test;

import util.Constants;

import java.sql.*;
import java.util.Enumeration;
import java.util.Scanner;

public class UserReg {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1. 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 查看所有的驱动
//        Enumeration<Driver> drivers = DriverManager.getDrivers();
//        while(drivers.hasMoreElements()){
//            Driver driver = drivers.nextElement();
//            System.out.println(driver);
//        }
        // 2. 获取数据库连接
        Connection conn = DriverManager.getConnection(Constants.URL, Constants.USERNAME, Constants.PASSWORD);
        // 获取要插入的数据
        Scanner in = new Scanner(System.in);
        System.out.printf("请输入用户名：");
        String username = in.next();
        System.out.printf("请输入密码：");
        String password = in.next();
        System.out.printf("请输入昵称：");
        String nickname = in.next();
        // 拼接要执行的sql
        String sql = "insert into userinfo (nickname,account,password) values ('" + nickname + "','" + username + "','" + password + "')";
        System.out.println(sql);
        // 3.创建执行sql 的对象
        Statement statement = conn.createStatement();
        // 4.执行sql语句
        int count = statement.executeUpdate(sql); // 执行非查询语句
        System.out.println("受影响的行数:" + count);
        // 5.释放资源 需要倒序关闭（先创建的后关闭 后创建的先关闭）
        statement.close();

        conn.close();

    }
}
